#!/usr/bin/python



from xpcom import components

import logging
logger = logging.getLogger("tags_synchronizer")



############################################################

class TagsSynchronizer:
	
	_com_interfaces_ = [components.interfaces.nsITagsSynchronizer]
	_reg_clsid_ = "{0d66c6fe-b7f3-40fa-ac82-2dd78938841d}"
	_reg_contractid_ = "@mozilla.org/TagsSynchronizer;1"

############################################################

	def importXulConstants(self):
		
		self.PATH_MAIN = components.classes['@mozilla.org/file/directory_service;1'].getService(components.interfaces.nsIProperties).get("ProfD", components.interfaces.nsILocalFile).path
		self.PATH_CONTENT = self.PATH_MAIN + "/extensions/nepomukfoxtrot@nepomuk.semanticdesktop.org/chrome/content/"
		import sys
		sys.path.append(self.PATH_CONTENT)
		import xul_constants
		
		import sys
		#logger.debug("sys.path = %s" % sys.path)
		from places_manager import PlacesManager
		
		self.places = PlacesManager()
		
		# tagging service
		self.tagssvc = components.classes["@mozilla.org/browser/tagging-service;1"].getService(components.interfaces.nsITaggingService)
		
		# bookmarks services
		self.bmsvc = components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].getService(components.interfaces.nsINavBookmarksService)
		
		# interface
		from python_interface_to_soprano import Interface
		self.interface = Interface()
		# This "/tmp/" folder is only a compatibility stuff. Indeed, the Interface object should not need a path any more. As soon as no files are used to get the results of some functions (now only remain the ones like getTagsOf that use an external generated tree to display graphical trees), this path stuff should be removed.
		
		# nao
		from nao import Nao
		self.nao = Nao(self.interface)

############################################################

	def synchronize(self, copy_ff_in_nepo, copy_nepo_in_ff):
		
		self.importXulConstants()
		
		logger.debug("synchronize(%s, %s)" % (copy_ff_in_nepo, copy_nepo_in_ff))
		
		ff_tags = self.places.getAllFFTags()
		nepomuk_tags = self.nao.getAllTagsSet()
		#logger.debug("ff_tags = %s" % ff_tags)
		#logger.debug("nepomuk_tags = %s" % nepomuk_tags)
		
		# remove the desc element from the nepomuk tags:
			# (uri, tag_label, title, desc) -> (uri, tag_label, title)
		for tag in nepomuk_tags:
			tag2 = (tag[0], tag[1], tag[2])
			nepomuk_tags.remove(tag)
			nepomuk_tags.add(tag2)
		
		supernumerary_tags_of_firefox = ff_tags - nepomuk_tags
		supernumerary_tags_of_nepomuk = nepomuk_tags - ff_tags
		
		logger.debug("supernumerary_tags_of_firefox = %s" % supernumerary_tags_of_firefox)
		logger.debug("supernumerary_tags_of_nepomuk = %s" % supernumerary_tags_of_nepomuk)
		
		from constants import NFO_WEBSITE
		
		if copy_ff_in_nepo != "None":
			
			for tag in supernumerary_tags_of_firefox:
				
				uri = tag[0]
				tag_label = tag[1]
				title = tag[2]
				
				if copy_ff_in_nepo == "True":
					# copy supernumerary_tags_of_firefox in nepomuk
					logger.debug("add (%s, %s, %s) in nepomuk" % (uri, tag_label, title))
					document_uri = self.interface.registerAndReify(uri, NFO_WEBSITE, title)
					self.nao.addTag(document_uri, tag_label)
				else:
					# remove supernumerary_tags_of_firefox from firefox
					logger.debug("remove (%s, %s, %s) from firefox" % (uri, tag_label, title))
					self.places.removeTag(uri, tag_label)
		
		
		if copy_nepo_in_ff != "None":
			
			for tag in supernumerary_tags_of_nepomuk:
				
				uri = tag[0]
				tag_label = tag[1]
				title = tag[2]
				
				if copy_nepo_in_ff == "True":
					# copy supernumerary_tags_of_nepomuk in firefox
					logger.debug("add (%s, %s, %s) in firefox" % (uri, tag_label, title))
					self.places.registerTag(uri, tag_label, title)
				else:
					# remove supernumerary_tags_of_nepomuk from nepomuk
					document_uri = self.interface.getDocument(uri)
					if document_uri != None:
						logger.debug("remove (%s, %s, %s) from nepomuk" % (uri, tag_label, title))
						logger.debug(self.nao.removeTag(document_uri, tag_label))
					else:
						logger.debug("ERROR: unable to remove (%s, %s, %s) from nepomuk because %s has no associated document." % (uri, tag_label, title, uri))

############################################################



















